package com.example.news_demo;


import com.example.news_demo.utils.DatabaseUtil;
import com.huaban.analysis.jieba.JiebaSegmenter;
import com.huaban.analysis.jieba.SegToken;
import jdk.internal.util.xml.impl.Input;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.io.*;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class InsertIntoVocabulary {

    @Test
    public void insert(){
        Connection conn = DatabaseUtil.getConnection();
        PreparedStatement pst = null;
        BufferedReader br = null;
        BufferedReader docno_br = null;
        int words;
        String sql = "update article_copy set words = ? where docno = ?";

        String file = "C:\\Users\\asus\\Desktop\\content.txt";
        String docno_file = "C:\\Users\\asus\\Desktop\\article_copy.txt";


        int i = 0;
        try {
            long startTime = System.currentTimeMillis();

            try {
                pst = conn.prepareStatement(sql);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
            InputStreamReader isr = null;
            InputStreamReader docno_isr = null;
            try {
                isr = new InputStreamReader(new FileInputStream(file),"UTF8");
                docno_isr = new InputStreamReader(new FileInputStream(docno_file),"UTF8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }

            br = new BufferedReader(isr);
            docno_br = new BufferedReader(docno_isr);
            try {
                String line = br.readLine();
                String docno_line = docno_br.readLine();
                while (line != null){
                    i++;
                    JiebaSegmenter segmenter = new JiebaSegmenter();
                    List<SegToken> tokenList = segmenter.process(line, JiebaSegmenter.SegMode.SEARCH);
                    words = tokenList.size();
                    try {
//                        System.out.println(words);
//                        System.out.println(docno_line);
                        pst.setInt(1,words);
                        pst.setString(2,docno_line);
//                        pst.setInt(2,i);
                        pst.addBatch();
                    } catch (SQLException throwables) {
                        throwables.printStackTrace();
                    }
                    /*for (String tmp : item) {
                        String[] strings = tmp.split("\\$");
                        //System.out.println(strings[0]+"--"+strings[1]);
                        //System.out.println(strings[0]+"--"+strings[1]);
                        try {
                            pst.setString(1,strings[0]);
                            pst.setString(2,strings[1]);
                            pst.addBatch();

                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }
                    }*/
                    if(i % 1000 == 0){
                        long currentTime = (System.currentTimeMillis() - startTime) /1000;
                        System.out.println(currentTime+"秒--"+i + "条记录");
                        try {
                            pst.executeBatch();
                            pst.clearBatch();
                        } catch (SQLException throwables) {
                            throwables.printStackTrace();
                        }
                    }
                    line = br.readLine();
                    docno_line = docno_br.readLine();
                }
            } catch (IOException e) {

                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                pst.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }

            try {
                conn.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }
   /* @Test
    void test1(){

    }*/
}
